package com.cwg.dict.ui.cli.impl;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.cwg.dict.model.History;

public class Main {

	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("app-context.xml");
		CommandLineInputImpl cli = (CommandLineInputImpl) ctx.getBean("CommandLineInput");
		querySomeWordForTesting(cli);

		try {
			InputStreamReader isr = new InputStreamReader(System.in);
			BufferedReader br = new BufferedReader(isr);
			while (true) {
				System.out.print("query for : ");
				String input = br.readLine();
				display(cli, input);
				displayWordList(cli, input);
				cli.getSpeaker().pronounce(input);
				displayHistory(cli);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void display(CommandLineInputImpl cli, String input) {
		cli.input(input);
		for (String s : cli.getOutput()) {
			System.out.println(s);
		}
	}

	private static void displayHistory(CommandLineInputImpl cli) {
		List<History> listTop = cli.getHistorical().listLatest(3);
		for (History history : listTop) {
			System.out.println("history : " + history);
		}
	}

	private static void displayWordList(CommandLineInputImpl cli, String input) {
		String[] wordList = cli.getDictManager().getWordList(input);
		int i = 1;
		for (String word : wordList) {
			System.out.printf("%05d : %s\n", i++, word);
		}
	}

	private static void querySomeWordForTesting(CommandLineInputImpl cli) {
		display(cli, "dishonest");
		display(cli, "atypical");
		display(cli, "amnesty");
		display(cli, "apathetic");
		display(cli, "apolitical");
		display(cli, "atheist");
		display(cli, "anonymous");

		display(cli, "antarctic");
		display(cli, "antidote");
		display(cli, "antipathy");

		display(cli, "contrary");
		display(cli, "contradict");
		display(cli, "controversial");
		display(cli, "counterfeit");

		display(cli, "unhappy");
		display(cli, "高興");
	}

}
